<?php
	require_once("/../globals.php");
	require_once("/ticket.php");
	// require_once("/../email/email.php");
	
	// if($_SERVER['REQUEST_METHOD'] == 'POST'):
		// if(isset($_POST['submitNewTicket'])):
			// createNewTicket();
		// endif;
	// elseif ($_SERVER['REQUEST_METHOD'] == 'GET'):
		
	// endif;
	
	function createNewTicket($firstname, $lastname, $email, $subject, $description)
	{
		require("private\mysqli_connect.php");
		// $firstname = sanitizeStringKeepSpaces($_POST['firstname']);
		// $lastname = sanitizeStringKeepSpaces($_POST['lastname']);	
		// $email = sanitizeStringKeepSpaces($_POST['email']);
		// $email = sanitizeString($email);
		// $subject = sanitizeStringKeepSpaces($_POST['subject']);
		// $description = sanitizeStringKeepSpaces($_POST['description']);
		if(!empty($firstname) && !empty($lastname) && !empty($email) && !empty($subject) 
				&& !empty($description)):
			$ticket_is_open = true;
			$id = -1;
			$administrator_id = -1;
			$received = (new DateTime())->format("Y-m-d h:m:s");
			$ticket = new Ticket($id, $received, $firstname, $lastname, $email, $subject, $description,
					$administrator_id, $ticket_is_open);
			$success = $db->query("insert into tickets(received, sender_firstname, sender_lastname, sender_email, subject, description, ticket_is_open)
								values (CURRENT_TIMESTAMP(), '$firstname', '$lastname', '$email', '$subject', '$description', $ticket_is_open)");
			//echo "new ticket: <br/><br/>" . var_dump($ticket) . "<br/>";
			if($success):
				// concatElementByID("errorMessage", "Successfully added ticket to database!</br>");
				sendEmailToUser($firstname, $lastname, $email, $subject, $description, true);
				sendEmailToAllAdmins($firstname, $lastname, $subject, $description, false);
			else:
				// concatElementByID("errorMessage", "There was trouble adding the ticket to the database.");
				//echo var_dump($ticket) . "</br></br>" . "$db->error";
			endif;
			return true;
		else:
			// concatElementByID("errorMessage", "All fields were not filled out.");
			// echo "createNewTicket(): one of inputs was empty\n";
			// echo "$firstname : $lastname : $email : $subject : $description";
			return false;
		endif;
	}
	
	function sendEmailToUser($firstname, $lastname, $email, $subject, $description, $displayMessages)
	{
		require_once("/../email/email.php");
		$receiver = $email;
		$message = "New ticket was created by $firstname $lastname:\n\n";
		$message .= "\tSubject of ticket: $subject";
		$message .= "\n\n\tDescription of problem: $description";
		$success = sendEmail("proj2noreply@pitt.edu", $receiver, "New Ticket: " . $subject, $message);
		if($success):
			if($displayMessages):
				$msg = "Sent email to your email address: $receiver.<br/>";
				// concatElementByID("errorMessage", $msg);
			endif;
			return true;
		else:
			if($displayMessages):
				$msg = "Error in sending email to your email address: $receiver.<br/>";
				// concatElementByID("errorMessage", $msg);
			endif;
			return false;			
		endif;
	}
	
	function sendEmailToAllAdmins($firstname, $lastname, $subject, $description, $displayMessages)
	{
		require("private\mysqli_connect.php");
		$result = $db->query("select email from users where is_admin = true");
		//echo var_dump($result) . "<br/>";
		$rows = $result->num_rows;
		if($rows > 0):
			for($i = 0; $i < $rows; $i++):
				$row = $result->fetch_array();
				$email = $row['email'];
				sendEmailToUser($firstname, $lastname, $email, $subject, $description, false);
				//echo $email . "<br/>";
			endfor;
			if($displayMessages):
				// concatElementByID("errorMessage", "Successfully sent email to users!</br>");
			endif;
			return true;
		else:
			if($displayMessages):
				// concatElementByID("errorMessage", "No administrators were added to the database.");
			endif;
			return false;
			//echo var_dump($ticket) . "</br></br>" . "$db->error";
		endif;
	}

?>
